package com.dm.cloud.images.mode;

import cn.hutool.core.bean.BeanUtil;
import com.github.dockerjava.api.model.*;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description ="Docker网络模型")
@Data
public class ContainerDetail {

    @Schema(description = "自定义节点ID")
    private String nodeId;

    @Schema(description = "自定义节点名称")
    private String nodeName;

    @Schema(description = "自定义节点IP")
    private String nodeHost;

    @Schema(description = "执行命令")
    private String command;

    @Schema(description = "创建时间")
    private Long created;

    @Schema(description = "容器ID")
    private String id;

    @Schema(description = "容器镜像")
    private String image;

    @Schema(description = "容器镜像ID")
    private String imageId;

    @Schema(description = "容器名称")
    private String[] names;

    @Schema(description = "容器使用的端口")
    public ContainerPort[] ports;

    @Schema(description = "容器的标签")
    public Map<String, String> labels;

    @Schema(description = "容器状态")
    private String status;

    private String state;

    private Long sizeRw;

    private Long sizeRootFs;

    @Schema(description = "本机设置")
    private ContainerHostConfig hostConfig;

    @Schema(description = "网络设置")
    private ContainerNetworkSettings networkSettings;

    @Schema(description = "挂载信息")
    private List<ContainerMount> mounts;

    @Schema(description = "关联应用ID")
    private String appCode;

    @Schema(description = "关联应用名称")
    private String appName;

    public static ContainerDetail buildByContainer(Container container,String nodeId,String nodeName,String nodeHost){
        ContainerDetail detail=new ContainerDetail();
        BeanUtil.copyProperties(container,detail);
        detail.setNodeId(nodeId);
        detail.setNodeName(nodeName);
        detail.setNodeHost(nodeHost);
        return detail;
    }

    public static  List<ContainerDetail> buildByContainerList(List<Container> containers,String nodeId,String nodeName,String nodeHost){
        List<ContainerDetail> containerList=new ArrayList<>();
        if(containers!=null){
            for (Container container : containers) {
                containerList.add(buildByContainer(container,nodeId,nodeName,nodeHost));
            }
        }
        return containerList;
    }

}
